<template>
  <ele-modal
    :width="820"
    :model-value="modelValue"
    :title="'当前学生:'+form.name+'('+form.code+')'+'所在系意见'"
    @update:modelValue="updateModelValue">
    <el-form :model="form" label-width="auto">
      <el-form-item label="所在系意见">
        <el-input v-model="form.departmentOpinion" type="textarea" rows="6"/>
      </el-form-item>
    </el-form>
    <el-divider style="margin-bottom: 0px;"/>
    <template #footer>
      <el-button @click="updateModelValue(false)">取消</el-button>
      <ele-popconfirm
        :width="256"
        placement="top-end"
        title="操作提示"
        content="当前学生已提交信息采集，是否直接审核该学生？"
        @confirm="saveAndCheck"
        @cancel="save"
        v-if="form.studentRecommendSubmit == '0' && form.counselorRecommendCheck == '1'"
      >
        <template #reference>
          <el-button type="primary" :loading="loading">
            提交
          </el-button>
        </template>
      </ele-popconfirm>

      <el-button type="primary" :loading="loading" @click="save" v-else>
        提交
      </el-button>
    </template>
  </ele-modal>
</template>


<script setup>
import {reactive, ref} from 'vue';
import {EleMessage} from 'ele-admin-plus/es';
import {useFormData} from "@/utils/use-form-data.js";
import {
  selectStudentRecommendInfo,
  fillStudentRecommendDepartmentOpinion,
  fillStudentRecommendDepartmentOpinionAndCheck
} from "@/api/manage/recommend/counsellor/index.js";

const emit = defineEmits(['done', 'update:modelValue']);
const props = defineProps({
  /** 弹窗是否打开 */
  modelValue: Boolean,
  /** 修改回显的数据 */
  data: String
});


/** 提交状态 */
const loading = ref(false);

const form = reactive({
  name: '',
  code: '',
  departmentOpinion: '',
  studentRecommendSubmit: '',
  counselorRecommendCheck: '',
})

/** 更新modelValue */
const updateModelValue = (value) => {
  emit('update:modelValue', value);
};


const selectCheckInfo = () => {
  loading.value = true;
  selectStudentRecommendInfo(props.data).then(response => {

    form.name = response.data.name;
    form.code = response.data.code;
    form.departmentOpinion = response.data.departmentOpinion;

    form.studentRecommendSubmit = response.data.studentRecommendSubmit;
    form.counselorRecommendCheck = response.data.counselorRecommendCheck;

    loading.value = false;
  });
}

const saveAndCheck = () => {
  if (!form.departmentOpinion) {
    EleMessage.error("请填写系所在意见");
    loading.value = false;
    return;
  }
  //如果当前学生已经提交信息采集，就提示是否直接审核学生
  loading.value = true;
  fillStudentRecommendDepartmentOpinionAndCheck(form.code, form.departmentOpinion)
    .then((msg) => {
      loading.value = false;
      EleMessage.success(msg);
      updateModelValue(false);
      emit('done');
    })
    .catch((e) => {
      loading.value = false;
      EleMessage.error(e.message);
    });
}

const save = () => {
  if (!form.departmentOpinion) {
    EleMessage.error("请填写系所在意见");
    loading.value = false;
    return;
  }
  loading.value = true;
  fillStudentRecommendDepartmentOpinion(form.code, form.departmentOpinion)
    .then((msg) => {
      loading.value = false;
      EleMessage.success(msg);
      updateModelValue(false);
    })
    .catch((e) => {
      loading.value = false;
      EleMessage.error(e.message);
    });
}

selectCheckInfo();
</script>
